package org.dcma.client;

import java.util.List;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.google.gwt.user.client.ui.ListBox;

public class DepartamentoGuiList {
	/* Constants. */
    private static final String DEPARTAMENTO_LISTING_ROOT_PANEL = "departamentoListing";
    private static final String DEPARTAMENTO_FORM_ROOT_PANEL = "departamentoForm";
    private static final String DEPARTAMENTO_STATUS_ROOT_PANEL = "departamentoStatus";
    private static final String DEPARTAMENTO_TOOL_BAR_ROOT_PANEL = "departamentoToolBar";
    private static final String DEPARTAMENTO_COMBO_ROOT_PANEL = "departamentoCombobox";
    private static final int EDIT_LINK = 1;
    private static final int REMOVE_LINK = 2;

    /* GUI Widgets */
    protected Button addButton;
    protected Button updateButton;
    protected Button addNewButton;
    protected TextBox nameField;    
    protected Label status;
    protected Grid departamentoGrid;
    protected Grid formGrid;
    protected ListBox cmbDepartamentos;
    
    /* Data model */
    private List<Departamento> departamentos;
    private Departamento currentDepartamento;
    protected DepartamentoServiceDelegate departamentoService;
        
    public void init() {
        addButton = new Button("[+] Departamento Nuevo");
        addNewButton = new Button("[+] Departamento Nuevo");
        updateButton = new Button("[*] Actualizar Departamento");
        cmbDepartamentos=new ListBox();
        nameField = new TextBox();        
        status = new Label();
        departamentoGrid = new Grid(2,3);

        buildForm();
        placeWidgets();
    }
    
    private void buildForm() {
        formGrid = new Grid(2,3);
        formGrid.setVisible(false);
        
        formGrid.setWidget(0, 0, new Label("Nombre"));
        formGrid.setWidget(0, 1, nameField);
       
        formGrid.setWidget(1, 0, updateButton);
        formGrid.setWidget(1, 1, addButton);
        
    }

    private void placeWidgets() {
        RootPanel.get(DEPARTAMENTO_LISTING_ROOT_PANEL).add(departamentoGrid);
        RootPanel.get(DEPARTAMENTO_FORM_ROOT_PANEL).add(formGrid);
        RootPanel.get(DEPARTAMENTO_STATUS_ROOT_PANEL).add(status);
        RootPanel.get(DEPARTAMENTO_TOOL_BAR_ROOT_PANEL).add(addNewButton);
        RootPanel.get(DEPARTAMENTO_COMBO_ROOT_PANEL).add(cmbDepartamentos);
    }

    private void loadForm(Departamento departamento) {
        this.formGrid.setVisible(true);
        currentDepartamento = departamento;        
        this.nameField.setText(departamento.getNombre());
    }


    private void copyFieldDateToDepartamento() {       
        currentDepartamento.setNombre(nameField.getText());        
    }

    public void gui_eventDepartamentoGridClicked(Cell cellClicked) {
         int row = cellClicked.getRowIndex();
         int col = cellClicked.getCellIndex();
        
         Departamento departamento = this.departamentos.get(row);
         this.status.setText("Name was " + departamento.getNombre() + " clicked ");
        
         if (col==EDIT_LINK) {
             this.addNewButton.setVisible(false);
             this.updateButton.setVisible(true);
             this.addButton.setVisible(false);             
             loadForm(departamento);
         } else if (col==REMOVE_LINK) {
             this.departamentoService.removeDepartamento(departamento);
         }
    }


    public void gui_eventAddButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToDepartamento();        
        this.departamentoService.addDepartamento(currentDepartamento);
    }

    public void gui_eventUpdateButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToDepartamento();
        this.departamentoService.updateDepartamento(currentDepartamento);
    }

    public void gui_eventAddNewButtonClicked() {
        this.addNewButton.setVisible(false);
        this.updateButton.setVisible(false);
        this.addButton.setVisible(true);        
        loadForm(new Departamento());
    }

    public void gui_eventCmbDepartamentosClicked() {
    	this.status.setText("El " + currentDepartamento.getNombre() + " fue elegido");
    }

    public void service_eventListRetrievedFromService(List<Departamento> result) {
        status.setText("Devuelta la lista de Departamentos ");
        this.departamentos = result;
        this.departamentoGrid.clear();
        this.departamentoGrid.resizeRows(this.departamentos.size());
        int row = 0;
        
        for (Departamento departamento : result) {
            this.departamentoGrid.setWidget(row, 0, new Label(departamento.getNombre().toUpperCase()));
            this.departamentoGrid.setWidget(row, EDIT_LINK, new Hyperlink("Edit", "#"));
            this.departamentoGrid.setWidget(row, REMOVE_LINK, new Hyperlink("Remove", "#"));
            this.cmbDepartamentos.addItem(departamento.getNombre(),departamento.getId().toString());
            row ++;
        }
    }

    public void service_eventAddDepartamentoSuccessful() {
        status.setText("Departamento was successfully added");
        this.departamentoService.listDepartamentos();
    }

    public void service_eventUpdateSuccessful() {
        status.setText("Departamento was successfully updated");
        this.departamentoService.listDepartamentos();
    }
    public void service_eventRemoveDepartamentoSuccessful() {
        status.setText("Departamento was removed");
        this.departamentoService.listDepartamentos();
        
    }

    public void service_eventUpdateDepartamentoFailed(Throwable caught) {
        status.setText("Update Departamento failed");
    }

    public void service_eventAddDepartamentoFailed(Throwable caught) {
        status.setText("Unable to update Departamento");
    }

    public void service_eventRemoveDepartamentoFailed(Throwable caught) {
        status.setText("Remove Departamento failed");
    }

    public void service_eventListDepartamentosFailed(Throwable caught) {
        status.setText("Unable to get Departamento list");
        
    }
}
